package authorization;

import utils.Utils;
import android.util.Log;

public class VkontakteAuth implements IAuthorization {
	private final String redirect_url = "https://oauth.vk.com/blank.html";
	private final String api_id = "3995007";
	private final String TAG = "VkontakteAuth";

	@Override
	public String getUrl() {
		String url = "";
		url = "https://oauth.vk.com/authorize?client_id=" + api_id
					+ "&display=mobile&scope=" + getSettings()
					+ "&redirect_uri=" + redirect_url
					+ "&response_type=token";
		return url;
	}

	@Override
	public String getSettings() {
		return "notify,photos,status,wall,groups,offline";
	}

	@Override
	public String[] parseRedirectUrl(String url) throws Exception {
		//url is something like http://api.vkontakte.ru/blank.html#access_token=66e8f7a266af0dd477fcd3916366b17436e66af77ac352aeb270be99df7deeb&expires_in=0&user_id=7657164
		String access_token=Utils.extractPattern(url, "access_token=(.*?)&");
        Log.i(TAG, "access_token=" + access_token);
        String user_id=Utils.extractPattern(url, "user_id=(\\d*)");
        Log.i(TAG, "user_id=" + user_id);
        if(user_id==null || user_id.length()==0 || access_token==null || access_token.length()==0)
            throw new Exception("Failed to parse redirect url "+url);
        return new String[]{access_token, user_id};
	}

	@Override
	public String getRedirectUrl() {
		return redirect_url;
	}
	
}
